const app = getApp();
var pageNo = 1;
var pageSize = 4;
var url = app.data.url;
var picRoot = app.data.picRoot;
import Dialog from '@vant/weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    merchantId: '',
    show: false,
    roomBtn: 0,
    name: '',
    desc: '',
    num: '',
    price: '',
    discountPrice: '',
    payType: '0',
    uuid: '',
    putAwayStatus: '',
    unitList: [],
    clotheClassShow: false,
    classNameValue: "",
    classDescValue: "",
    fileList: [],
    fileSavePath: '',
    fileSavePathId: '',
    updateFileSavePathId: '',
    method: 0,
    id: '',
    overLayShow: false,
    putAwayStatus: true
  },

  onPutAwayStatusChange: function (event) {
    let _that = this;
    _that.setData({
      putAwayStatus: event.detail
    });
  },

  /**跳转衣服明细页面 */
  toClothesType: function (option) {
    let _that = this;
    let merchantId = _that.data.merchantId;
    let id = option.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/car-type-detail/index?merchantId=' + merchantId + "&classId=" + id
    })
  },

  /**保存或者更新 */
  insertOrUpdateClothesClass: function () {
    let _that = this;
    let method = _that.data.method;
    let name = _that.data.classNameValue;
    let desc = _that.data.classDescValue;
    let merchantId = _that.data.merchantId;
    let fileSavePath = _that.data.fileSavePath;
    let fileSavePathId = _that.data.fileSavePathId;
    let putAwayStatus = _that.data.putAwayStatus ? 1 : 0
    let uuid = _that.data.uuid;
    let tempUrl;
    let tempData;
    if (method == 0) {
      tempUrl = url + '/ClothesSystemApi/addClothesClass';
      tempData = {
        name: name,
        description: desc,
        merchantId: merchantId,
        createUserName: uuid,
        updateUserName: uuid,
        path: fileSavePath,
        fileSavePathId: fileSavePathId,
        status: putAwayStatus
      }
    } else {
      tempUrl = url + '/ClothesSystemApi/updateClothesClass';
      let id = _that.data.id;
      fileSavePathId = _that.data.updateFileSavePathId;
      tempData = {
        id: id,
        name: name,
        description: desc,
        merchantId: merchantId,
        createUserName: uuid,
        updateUserName: uuid,
        path: fileSavePath,
        fileSavePathId: fileSavePathId,
        status: putAwayStatus
      }
    }
    wx.request({
      url: tempUrl,
      data: tempData,
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res);
        if (res.data.code == 200) {
          _that.setData({
            clotheClassShow: false
          })
          pageNo = 1;
          pageSize = 10;
          _that.queryData(pageNo, pageSize, merchantId);
          wx.showToast({
            title: '操作成功!',
            icon: "success",
            duration: 2000
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none",
            duration: 2000
          })
        }

      },
      fail: function (res) {
        console.info('submit fail');
      },
      complete: function (res) {
        console.info('submit complete');
      }
    })

  },

  /**弹框 */
  onOverLayShow() {
    this.setData({
      overLayShow: true
    });
  },
  /**弹框 */
  onOverLayHide() {
    this.setData({
      overLayShow: false
    });
  },
  /**
   * 上传图片
   */
  afterRead(event) {
    let _that = this;
    const {
      file
    } = event.detail;
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    _that.onOverLayShow();
    wx.uploadFile({
      url: url + '/UploadRecordApi/uploadFile?bucketName=clothes',
      filePath: file.path,
      header: {
        "Content-Type": "multipart/form-data"
      },
      name: 'file',
      success(res) {
        let result = res.data;
        result = JSON.parse(result);
        if (res.statusCode == 200) {
          // 上传成功
          const {
            fileList = []
          } = _that.data;
          fileList.push({
            ...file,
            url: result.data.path
          });
          _that.setData({
            fileList
          });
          _that.setData({
            fileSavePath: result.data.path,
            fileSavePathId: result.data.id,
            updateFileSavePathId: result.data.id
          })
        } else {
          wx.showToast({
            title: "上传失败!",
            icon: "none",
            duration: 2000
          })
        }
        _that.onOverLayHide();
      }
    });

  },


  /**
   * 删除图片
   */
  deleteImg: function (option) {
    let _that = this;
    Dialog.confirm({
      title: '是否删除',
      message: '删除后请点击编辑按钮保存!'
    }).then(() => {
      // on confirm
      let {
        fileList = []
      } = _that.data;
      fileList.splice(option.detail.index, 1);
      _that.setData({
        fileList: fileList,
        fileSavePath: ''
      });
    }).catch(() => {
      // on cancel
    });

  },
  /**获取品类值 */
  onClassDescValueChange: function (option) {
    let _that = this;
    _that.setData({
      classDescValue: option.detail
    })
  },
  /**获取品类值 */
  onClassValueChange: function (option) {
    let _that = this;
    _that.setData({
      classNameValue: option.detail
    })
  },
  /**打开新增品类弹框 */
  openClass: function (option) {
    let _that = this;
    let id = option.currentTarget.dataset.id;
    let method = option.currentTarget.dataset.method;
    if (method == 0) {
      // 新增
      _that.setData({
        classNameValue: '',
        classDescValue: "",
        fileList: [],
      })
    } else {
      // 编辑
      wx.request({
        url: url + '/ClothesSystemApi/queryClothesById',
        data: {
          id: id
        },
        method: 'POST',
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          console.log("=========");
          console.log(res);
          if (res.data.code == 200) {
            let item = res.data.data;
            let fileList = [];
            fileList.push({
              url: item.path
            });
            _that.setData({
              id: item.id,
              updateFileSavePathId: item.imgPathId,
              classNameValue: item.name,
              classDescValue: item.description,
              fileList: fileList,
              fileSavePath: item.path
            })
            console.log(fileList);
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: "none",
              duration: 2000
            })
          }
        }
      })
    }
    _that.setData({
      clotheClassShow: true,
      method: method
    })
  },
  /**关闭新增品类弹框 */
  closeClass: function (option) {
    let _that = this;
    _that.setData({
      clotheClassShow: false
    })
  },
  toShowCar: function (option) {
    let _that = this;
    let merchantId = _that.data.merchantId;
    let classId = _that.data.id;
    wx.navigateTo({
      url: '/pages/show-car/index?merchantId=' + merchantId + "&classId=" + classId + '&descType=14'
    })
  },
  /**
   * 删除
   */
  deleteUnit: function (option) {
    let _that = this;
    let item = option.currentTarget.dataset.item;
    let id = item.id;
    console.log(id);
    let merchantId = _that.data.merchantId;
    console.log(merchantId);
    let title = item.name;
    Dialog.confirm({
      title: '提醒',
      message: '是否确定删除【' + title + "】"
    }).then(() => {
      wx.request({
        url: url + '/ClothesSystemApi/deleteById',
        data: {
          id: id
        },
        method: 'POST',
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          if (res.data.code == 200) {
            pageNo = 1;
            pageSize = 10;
            _that.queryData(pageNo, pageSize, merchantId, );
            wx.showToast({
              title: "删除成功!",
              icon: "success",
              duration: 2000
            })
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: "none",
              duration: 2000
            })
          }
        }
      })
    }).catch(() => {
      // on cancel
    });

  },
  /**
   * 加载衣服品类
   */
  queryData: function (pageNo, pageSize, merchantId, flag) {
    let _that = this;
    wx.request({
      url: url + '/ClothesSystemApi/listPageClothesClass',
      data: {
        pageNo: pageNo,
        pageSize: pageSize,
        merchantId: merchantId,
        invoking: 1
      },
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        if (res.data.code == 200) {
          console.log(res.data.data);
          if (!flag) {
            _that.setData({
              unitList: res.data.data.pageList
            })
          } else {
            if (res.data.data.pageList.length > 0) {
              _that.setData({
                unitList: _that.data.unitList.concat(res.data.data.pageList)
              })
            }
          }
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none",
            duration: 2000
          })
        }
      },
      fail: function (res) {
        console.info('submit fail');
      },
      complete: function (res) {
        console.info('submit complete');
      }
    })
  },
  /**表单start */
  onNameChange: function (event) {
    let _that = this;
    _that.setData({
      name: event.detail
    })
  },
  onDescChange: function (event) {
    let _that = this;
    _that.setData({
      desc: event.detail
    })
  },
  onNumChange: function (event) {
    let _that = this;
    _that.setData({
      num: event.detail
    })
  },
  onPriceChange: function (event) {
    let _that = this;
    _that.setData({
      price: event.detail
    })
  },
  onDiscountPriceChange: function (event) {
    let _that = this;
    _that.setData({
      discountPrice: event.detail
    })
  },
  onRadioChange: function (event) {
    let _that = this;
    _that.setData({
      payType: event.detail
    });
  },

  /**表单end */
  showPopup: function (option) {
    console.log(option);
    let _that = this;
    let roomBtn = option.currentTarget.dataset.roombtn;
    let item = option.currentTarget.dataset.item;
    if (roomBtn == 1) {
      // 回显
      let temp = item.putAwayStatus == 0 ? true : false;
      _that.setData({
        name: item.title,
        desc: item.description,
        num: item.num,
        price: item.originalPrice,
        discountPrice: item.discountPrice,
        payType: item.payType + '',
        putAwayStatus: temp,
        roomBtn: 1,
        id: item.id
      })
    }
    _that.setData({
      show: true
    })
  },
  onPutAwayStatusChange: function (event) {
    console.log(event);
    let _that = this;
    _that.setData({
      putAwayStatus: event.detail
    });
  },
  closePopup() {
    let _that = this;
    _that.setData({
      show: false
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let merchantId = options.merchantId;
    console.log(merchantId);
    pageNo = 1;
    // let merchantId = 38;
    let uuid = app.globalData.uuid
    let _that = this;
    _that.setData({
      merchantId: merchantId,
      uuid: uuid
    })
    _that.queryData(pageNo, pageSize, merchantId);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    pageNo = 1;
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    pageNo = pageNo + 1;
    let merchantId = that.data.merchantId;
    that.queryData(pageNo, pageSize, merchantId, true);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})